package cn.piesat.service.impl;

import cn.piesat.dao.AgreementDAO;
import cn.piesat.dto.AgreementDTO;
import cn.piesat.service.intf.AgreementService;
import cn.piesat.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 	注册协议服务实现
 * @author 奚好雷
 *
 */
@Service
public class AgreementServiceImpl implements AgreementService {

	@Autowired
	private AgreementDAO agreementDAO;
	
	@Override
	public AgreementDTO add(AgreementDTO dto) {
		AgreementDTO reDto = agreementDAO.save(dto);
		return reDto;
	}

	@Override
	public List<AgreementDTO> findByState(Integer state) {
		return agreementDAO.findByEnableState(state);
	}

	@Override
	public Map<String, Object> findAll(Integer pageNum,Integer pageSize) {
		Pageable pageable = PageRequest.of(pageNum, pageSize);
		Page<AgreementDTO> page = agreementDAO.findByOrderByProtocolType(pageable);
		return PageUtil.getPage(page);
	}

	@Override
	public AgreementDTO update(AgreementDTO dto) {
		AgreementDTO reDto = agreementDAO.save(dto);
		return reDto;
	}

	@Override
	public void del(String id) {
		agreementDAO.deleteById(id);
	}

	@Override
	public List<AgreementDTO> findByProtocolType(String protocolType) {
		return agreementDAO.findByProtocolType(protocolType);
	}

	@Override
	public AgreementDTO findById(String id) {
		Optional<AgreementDTO> optional = agreementDAO.findById(id);
		return optional.orElse(null);
	}

}
